package com.ocom.webapi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.constant.WebApiServiceUrl;
import com.ocom.common.entity.card.EntityXfAccountType;
import com.ocom.common.entity.card.EntityXfMealInfo;
import com.ocom.common.entity.payconfig.EntityXfDiscountConfig;
import com.ocom.common.request.web.ConsDiscountConfigOnShowRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.webapi.DiscountVo;
import com.ocom.common.vo.webapi.MealListVo;
import com.ocom.security.authentication.YoCiUser;
import com.ocom.security.utils.SecurityUtils;
import com.ocom.webapi.feign.PayConfigClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: TODO
 * @Author: 朱晖
 * @Date: 2022/7/21
 **/
@RestController
@PreAuthorize("@pms.hasRole('ROLE_WEB')" )
public class ConsDiscountConfigController {


	@Autowired
	PayConfigClientService payConfigClientService;

	//3.10.1消费优惠参数列表（ConsDiscountConfig/onShow）
	@PostMapping(WebApiServiceUrl.URL_ConsDiscountConfigOnShow)
	public Result ConsDiscountConfigOnShow(@Valid @RequestBody ConsDiscountConfigOnShowRequest request){
		YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
		request.setComId(yoCiUser.getComId());
		Map<String, Object> map =new HashMap<>();

		Result discountList =  payConfigClientService.getConsDiscountConfig(request.getComId(),
				request.getAccountType(),
				request.getConsTimeZone());
		//消费类型
		Result consTypeList =  payConfigClientService.getConsType(request.getComId());

//		Map<Integer,String > stringMap = new HashMap<>();
//		if(consTypeList.getCode()==200){
//			List<EntityConsTypeConfig> entityConsTypeConfigList = JSON.parseObject(JSON.toJSONString(consTypeList.getData()),List.class);
//			stringMap = entityConsTypeConfigList.stream()
//					.collect(Collectors.toMap(EntityConsTypeConfig::getConsType, EntityConsTypeConfig::getConsName));
//		}

		//账户类型
		Result accountTypeList = payConfigClientService.GetXFAccountType(request.getComId());
		Result mealList = payConfigClientService.GetMealNameInfo(request.getComId());

//		Map<Integer,String > stringMap1  = new HashMap<>();
//		if(mealList.getCode()==200){
//			List<EntityConsTypeConfig> entityConsTypeConfigList = JSON.parseObject(JSON.toJSONString(mealList.getData()),List.class);
//			stringMap1 = entityConsTypeConfigList.stream()
//					.collect(Collectors.toMap(EntityConsTypeConfig::getConsType, EntityConsTypeConfig::getConsName));
//		}

		Integer accountType = request.getAccountType();

		if(accountTypeList.getCode()==200 && request.getAccountType()==0){
			List<EntityXfAccountType> list = JSON.parseObject(JSON.toJSONString(accountTypeList.getData()),List.class);
			if(list!=null  && list.size()>0){
				JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(list.get(0)));
				accountType= jsonObject.getInteger("accTypeId");
			}
		}

		List<EntityXfMealInfo> list1=new ArrayList<>();

		if(mealList.getCode()==200){
			MealListVo mealListVo = JSON.parseObject(JSON.toJSONString(mealList.getData()),MealListVo.class);
			list1 = mealListVo.getList();
		}

		List<DiscountVo> list;
		//存在数据
		if(discountList.getData()!=null){
			list=JSON.parseObject(JSON.toJSONString(discountList.getData()),List.class);
		}


		map.put("consTypeList",consTypeList.getData());
		map.put("accountTypeList",accountTypeList.getData());
		map.put("mealList",list1);
		map.put("accountType",accountType);
		map.put("discountList",discountList.getData());
		return ResultUtil.success(map);
	}

	@PostMapping(WebApiServiceUrl.URL_ConsDiscountConfigBOnShow)
	public Result ConsDiscountConfigBOnShow(@Valid @RequestBody ConsDiscountConfigOnShowRequest request){

		Map<String, Object> map =new HashMap<>();

		Result discountList =  payConfigClientService.getConsDiscountConfig(request.getComId(),
				request.getAccountType(),
				request.getConsTimeZone());
		//消费类型
		Result consTypeList =  payConfigClientService.getConsType(request.getComId());

		//账户类型
		Result accountTypeList = payConfigClientService.GetXFAccountType(request.getComId());
		Result mealList = payConfigClientService.GetMealNameInfo(request.getComId());

		Integer accountType = request.getAccountType();

		if(accountTypeList.getCode()==200 && request.getAccountType()==0){
			List<EntityXfAccountType> list = JSON.parseObject(JSON.toJSONString(accountTypeList.getData()),List.class);
			if(list!=null  && list.size()>0){
				JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(list.get(0)));
				accountType= jsonObject.getInteger("accTypeId");
			}
		}

		List<EntityXfMealInfo> list1=new ArrayList<>();

		if(mealList.getCode()==200){
			MealListVo mealListVo = JSON.parseObject(JSON.toJSONString(mealList.getData()),MealListVo.class);
			list1 = mealListVo.getList();
		}

		map.put("consTypeList",consTypeList.getData());
		map.put("accountTypeList",accountTypeList.getData());
		map.put("mealList",list1);
		map.put("accountType",accountType);
		map.put("discountList",discountList.getData());
		return ResultUtil.success(map);
	}

	//3.10.2修改消费优惠参数（ConsDiscountConfig/modify）
	@PostMapping(WebApiServiceUrl.URL_ConsDiscountConfigModify)
	public Result ConsDiscountConfigModify(@RequestBody EntityXfDiscountConfig entityXfDiscountConfig){
		Integer merchId = entityXfDiscountConfig.getMerchId() ;
		merchId = merchId!=null?merchId:0;
		YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
		if(yoCiUser.getRole()==41l || yoCiUser.getRole()==42l || yoCiUser.getMerchId()>0l){
			merchId= yoCiUser.getMerchId().intValue();
		}
		entityXfDiscountConfig.setMerchId(merchId);
		if(entityXfDiscountConfig.getConsTimeZone()==null){
			entityXfDiscountConfig.setConsTimeZone(0);
		}

		if (null == entityXfDiscountConfig.getAccTypeId()) {
			entityXfDiscountConfig.setAccTypeId(entityXfDiscountConfig.getAccountType());
		}

		return payConfigClientService.setConsDiscountConfig(entityXfDiscountConfig);
	}

	@PostMapping(WebApiServiceUrl.URL_ConsDiscountConfigBModify)
	public Result ConsDiscountConfigBModify(@RequestBody EntityXfDiscountConfig entityXfDiscountConfig){
		Integer merchId = entityXfDiscountConfig.getMerchId() ;
		merchId = merchId!=null?merchId:0;
		YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
		if(yoCiUser.getRole()==41l || yoCiUser.getRole()==42l || yoCiUser.getMerchId()>0l){
			merchId= yoCiUser.getMerchId().intValue();
		}
		entityXfDiscountConfig.setMerchId(merchId);
		if(entityXfDiscountConfig.getConsTimeZone()==null){
			entityXfDiscountConfig.setConsTimeZone(0);
		}
		if (null == entityXfDiscountConfig.getAccTypeId()) {
			entityXfDiscountConfig.setAccTypeId(entityXfDiscountConfig.getAccountType());
		}
		return payConfigClientService.setConsDiscountConfigB(entityXfDiscountConfig);
	}

	//3.10.3删除消费优惠参数（ConsDiscountConfig/remove）
	@PostMapping(WebApiServiceUrl.URL_ConsDiscountConfigRemove)
	public Result ConsDiscountConfigRemove(@RequestBody EntityXfDiscountConfig entityXfDiscountConfig){
		Integer merchId = entityXfDiscountConfig.getMerchId() ;
		merchId = merchId!=null?merchId:0;
		YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
		if(yoCiUser.getRole()==41l || yoCiUser.getRole()==42l || yoCiUser.getMerchId()>0l){
			merchId= yoCiUser.getMerchId().intValue();
		}
		entityXfDiscountConfig.setMerchId(merchId);
		if(entityXfDiscountConfig.getConsTimeZone()==null){
			entityXfDiscountConfig.setConsTimeZone(0);
		}
		if (null == entityXfDiscountConfig.getAccTypeId()) {
			entityXfDiscountConfig.setAccTypeId(entityXfDiscountConfig.getAccountType());
		}
		return payConfigClientService.removeConsDiscountConfig(entityXfDiscountConfig);
	}

	@PostMapping(WebApiServiceUrl.URL_ConsDiscountConfigBRemove)
	public Result ConsDiscountConfigBRemove(@RequestBody EntityXfDiscountConfig entityXfDiscountConfig){
		Integer merchId = entityXfDiscountConfig.getMerchId() ;
		merchId = merchId!=null?merchId:0;
		YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
		if(yoCiUser.getRole()==41l || yoCiUser.getRole()==42l || yoCiUser.getMerchId()>0l){
			merchId= yoCiUser.getMerchId().intValue();
		}
		entityXfDiscountConfig.setMerchId(merchId);
		if(entityXfDiscountConfig.getConsTimeZone()==null){
			entityXfDiscountConfig.setConsTimeZone(0);
		}
		if (null == entityXfDiscountConfig.getAccTypeId()) {
			entityXfDiscountConfig.setAccTypeId(entityXfDiscountConfig.getAccountType());
		}
		return payConfigClientService.removeConsDiscountConfigB(entityXfDiscountConfig);
	}
}
